package com.chao.deeply.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/8/8
 */
public class LockSupportDemo {

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis() + "【主线程】开始执行！");
        Thread thread = new Thread(new LockSupportTask());
        thread.start();
        System.out.println(System.currentTimeMillis() + "【主线程】执行LockSupport.unPark()，释放子线程的阻塞！");
        LockSupport.unpark(thread);
        System.out.println(System.currentTimeMillis() + "【主线程】运行结束！");
    }

    static class LockSupportTask implements Runnable {

        @Override
        public void run() {
            System.out.println(System.currentTimeMillis() + "【子线程】开始运行！");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(System.currentTimeMillis() + "【子线程】执行LockSupport.park()方法，阻塞本线程！");
            // 将thread的一个许可从可用状态转化为不可用状态
            LockSupport.park();
            System.out.println(System.currentTimeMillis() + "【子线程】运行结束！");
        }
    }
}
